package com.mcb_wxc.util;


import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
 * Created by 小生
 * 2019/2/23 10:59.
 * 读文字
 */
public class Voice {

    public static void main(String[] args) throws IOException {

        ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");

        Dispatch sapo = sap.getObject();
        try {
            // 音量 0-100
            sap.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10
            sap.setProperty("Rate", new Variant(2));
            // 执行朗读
            Dispatch.call(sapo, "Speak", new Variant("对的人别错过，错的人别执着！"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sapo.safeRelease();
            sap.safeRelease();
        }
    }

    /**
     * 立体音 分离 成单声道（立体语音是8kAlaw）
     *
     * @param stereoBytes
     * @return
     * @throws IOException
     */
    public static byte[][] stereo2MonoForAlaw(byte[] stereoBytes) throws IOException {

        byte[][] objList = new byte[2][];

        try (ByteArrayOutputStream outputStreamL = new ByteArrayOutputStream()) {
            try (ByteArrayOutputStream outputStreamR = new ByteArrayOutputStream()) {

                for (int i = 0; i < stereoBytes.length; i = i + 2) {
                    outputStreamL.write(stereoBytes[i]);
                    outputStreamR.write(stereoBytes[i + 1]);
                }
                outputStreamL.flush();
                outputStreamR.flush();
                objList[0] = outputStreamL.toByteArray();
                objList[1] = outputStreamR.toByteArray();
            }
        }
        return objList;
    }

}
